//
//  UUHybridSelectPeakSeasonHandler.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 27/02/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUHybridSelectPeakSeasonHandler.h"
#import "UUSupplierPeakSeasonPicker.h"
#import "UUSupplierManager.h"

@implementation UUHybridSelectPeakSeasonHandler

- (NSString *)name {
    return @"selectPeak";
}

- (void)executeWithParams:(id)params callbackWith:(void (^)(id))callback {
    NSArray *data = params[@"data"];
    NSMutableArray *selectedPeakSeasonIds = [NSMutableArray new];
    for (NSDictionary *dict in data) {
        [selectedPeakSeasonIds addObject:dict[@"id"]];
    }
    
    UIViewController *picker = [UUSupplierPeakSeasonPicker pickerWithSelectedPeakSeasonIds:selectedPeakSeasonIds completeBlock:^(NSArray<NSNumber *> *peakSeasonIds) {
        NSMutableArray<NSDictionary *> *selectedPeakSeasons = [NSMutableArray arrayWithCapacity:peakSeasonIds.count];
        for (NSNumber *peakSeasonId in peakSeasonIds) {
            UUSupplierPeakSeasonModel *peakSeason = [GetSupplierManager() fetchPeakSeasonModelWithId:peakSeasonId];
            [selectedPeakSeasons addObject:@{
                                             @"id" : peakSeason.id,
                                             @"name" : peakSeason.name,
                                             }];
        }
        callback(selectedPeakSeasons);
    }];
    
    [self.context.hostVC presentViewController:picker animated:YES completion:nil];
}

@end
